﻿using System;
using System.Data;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyTuyenSinh.BussinessLayer;
using QuanLyTuyenSinh.Properties;

namespace QuanLyTuyenSinh.PresentationLayer
{
    public partial class frmStudy : Office2007Form
    {
        private int ma;

        public frmStudy()
        {
            InitializeComponent();
        }
        private void HideControl(bool hien)
        {
            cmdThem.Visible = hien;
            cmdLuu.Visible = hien;
            cmdXoa.Visible = hien;
            cmdThem.Enabled = true;
            cmdXoa.Enabled = true;
        }
        private void FrmNganhThiLoad(object sender, EventArgs e)
        {
           
            DataTable listUniversity = Operator.Operator.GetListUniversity();
            if (listUniversity.Rows.Count > 0)
            {
                cbTruong.DataSource = listUniversity;
                cbTruong.ValueMember = "Id";
                cbTruong.DisplayMember = "UniversityName";
            }
            else
            {
                MessageBox.Show("Chưa cập nhật danh sách trường ĐH");

                var frm = new frmUniversity() { Name = typeof(frmUniversity).Name };
                frm.Visible = false;
                this.Close();

                frm.ShowDialog();

            }
            DataTable listStudy = Operator.Operator.GetListStudy();
            if (listStudy.Rows.Count > 0)
            {
                GrNganh.DataSource = listStudy;
            }
            listKhoi.Items.Clear();
          txtKhoithi.Clear();
          HideControl(false);
            cmdThem.Visible = true;
        }

   private void SetDataKhoithi()
        {
            if (listKhoi.Items.Count == 1)
            {
                listKhoi.SelectedIndex = 0;
                txtKhoithi.Text = listKhoi.SelectedItem.ToString();
            }
            else
            {
                txtKhoithi.Text = "";
                while (listKhoi.Items.Count > 1)
                {
                    listKhoi.SelectedIndex = 0;
                    txtKhoithi.Text += listKhoi.SelectedItem + ",";
                    listKhoi.Items.RemoveAt(0);
                }
                listKhoi.SelectedIndex = 0;
                txtKhoithi.Text += listKhoi.SelectedItem.ToString();
                listKhoi.Items.RemoveAt(0);
            }
           }
        private void CmdThemClick(object sender, EventArgs e)
        {
            listKhoi.Items.Clear();
            txtKhoithi.Clear();
            cbTruong.Focus();
            txtManganh.Clear();
            txtTennganh.Clear();
            HideControl(true);
            ma = 0;
            cmdXoa.Visible = false;
            cbKhoithi.SelectedIndex = 1;
        }
          

       public void LoadKhoithi()
        {
            string[] a = txtKhoithi.Text.Split(',');
            for (int i = 0; i < a.Length; i++)
            {
                listKhoi.Items.Add(a[i]);
            }
        }

        private void CmdLuuClick(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtManganh.Text.Trim()))
                {
                    MessageBox.Show("Chưa nhập: Mã ngành");
                    txtManganh.Focus();
                    return;
                }
                var ethnic = new Study();
                ethnic.StudyDisplay = txtManganh.Text.Trim();
                ethnic.StudyName = txtTennganh.Text.Trim();
                if (listKhoi.Items.Count<=0)
                {
                    MessageBox.Show("Chưa chọn: Khối thi");
                    return;
                }
                SetDataKhoithi();
                ethnic.BlockName = txtKhoithi.Text.Trim();
               
                ethnic.UniversityId = (int)cbTruong.SelectedValue;
                DataTable listEthnic = Operator.Operator.GetListStudyById(ma);
                if (listEthnic.Rows.Count > 0)
                {
                    ethnic.Id = ma;
                    int m = ethnic.Update(ethnic);
                    if (m > 0)
                    {
                        FrmNganhThiLoad(sender, e);
                    }
                }
                else
                {
                    int m = ethnic.Insert(ethnic);
                    if (m > 0)
                    {
                        FrmNganhThiLoad(sender, e);
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
        }

        private void GrNganhCellClick(object sender, DataGridViewCellEventArgs e)
        {
            listKhoi.Items.Clear();
            DataGridViewRow dong = new DataGridViewRow();
                     
            if (e.RowIndex >= 0)
            {
                dong = GrNganh.Rows[e.RowIndex];
               ma = int.Parse(dong.Cells[0].Value.ToString());
                cbTruong.SelectedValue = dong.Cells[1].Value.ToString();
                txtKhoithi.Text = dong.Cells[4].Value.ToString();
                txtManganh.Text = dong.Cells[2].Value.ToString();
                txtTennganh.Text = dong.Cells[3].Value.ToString();
              }
            LoadKhoithi();
            txtKhoithi.Clear();
            HideControl(true);
        }


        private void CmdXoaClick(object sender, EventArgs e)
        {
            try
            {
                DialogResult DRL;
                DRL = MessageBox.Show(Resources.Delete, Resources.Header, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (DRL == DialogResult.No)
                {
                    return;
                }
                else
                {
                    var ethnic = new Study();
                    ethnic.Id = ma;
                    int m = ethnic.Delete(ethnic);
                    if (m > 0)
                    {
                        FrmNganhThiLoad(sender, e);
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
        }

        private void FrmNganhThiKeyDown(object sender, KeyEventArgs e)
        {
            if ((e.Control) && (e.KeyCode == Keys.S) && cmdLuu.Visible)
            {
                CmdLuuClick(sender, e);
            }
            else if (e.Control && e.KeyCode == Keys.N)
            {
                CmdThemClick(sender, e);
            }
            else if ((e.KeyCode == Keys.Delete) && (cmdXoa.Visible))
            {
                CmdXoaClick(sender, e);
            }
        }

        private void BtnAddClick(object sender, EventArgs e)
        {
            if(cbKhoithi.Items.Count>0)
                {
                    if (cbKhoithi.SelectedItem==null)
                    {
                        MessageBox.Show("Bạn hiện đang không chọn khối nào trong list");
                        return;
                    }
            listKhoi.Items.Add(cbKhoithi.SelectedItem);
                cbKhoithi.Items.Remove(cbKhoithi.SelectedItem);
                }
            else
            {
                MessageBox.Show("Hết khối thi để chọn");
            }

        }

        private void BtnDeleteClick(object sender, EventArgs e)
        {
            if (listKhoi.SelectedIndices.Count == 0)
            {
                MessageBox.Show("Bạn hiện đang không chọn khối nào trong list");
                return;
            }
            else
            {
                if(listKhoi.Items.Count>0)
                listKhoi.Items.RemoveAt(listKhoi.SelectedIndex);
                else MessageBox.Show("Hết khối thi để remove");
            }
        }

      }
}